package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;

import java.util.List;

import utils.Utils;

public class LuziItemAdapter extends RecyclerView.Adapter<LuziItemAdapter.ViewHolder> {
    private static final String TAG = LuziItemAdapter.class.getSimpleName();
    private final int width;
    private Context mContext;
    private List<String> mList;
    private LayoutInflater inflater;


    public LuziItemAdapter(Context context, List<String> list) {
        inflater = LayoutInflater.from(context);
        this.mContext = context;
        this.mList = list;
        while (mList.size() < 18 * 6) {
            mList.add("");
        }
        while (mList.size() > 150) {
            mList.remove(0);
        }
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.luzi_grid_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        String luzi = mList.get(position);
        holder.mTextView.setText(luzi.equals("*") ? "" : luzi);

        if (luzi.equals("单") || luzi.equals("虎") || luzi.equals("小")) {
            holder.mTextView.setBackgroundColor(mContext.getResources().getColor(R.color.luzi_red));
        } else if (luzi.equals("双") || luzi.equals("龙") || luzi.equals("大")) {
            holder.mTextView.setBackgroundColor(mContext.getResources().getColor(R.color.luzi_blue));
        } else if (luzi.equals("和")) {
            holder.mTextView.setBackgroundColor(mContext.getResources().getColor(R.color.green));
        } else if (luzi.equals("*") || Utils.isEmpty(luzi)) {
            holder.mTextView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        } else {
            if (!luzi.equals("*")) {
                holder.mTextView.setBackgroundColor(mContext.getResources().getColor(R.color.luzi_red));
            }
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTextView;
        FrameLayout flGridItemName;
        ViewHolder(View itemView) {
            super(itemView);
            flGridItemName=   itemView.findViewById(R.id.fl_grid_item_name);
            mTextView = itemView.findViewById(R.id.tv_grid_item_name);
                //int maxWidth=(width-19);
                int perWidth=width/18;
                ViewGroup.LayoutParams params=flGridItemName.getLayoutParams();
                params.width=perWidth;
                params.height=perWidth;
             flGridItemName.setLayoutParams(params);
            setIsRecyclable(false);
        }
    }
}
